package l1j.server.server.model;

import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;

import l1j.server.server.GeneralThreadPool;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_EffectLocation;

public class L1MagicEffect extends TimerTask{

	private ScheduledFuture<?> _future = null;
	private int _timeCounter = 0;
	private final L1Character _cha;
	private final int _gfxid;
	private final L1PcInstance _pc;
	
	public L1MagicEffect(L1PcInstance pc, L1Character cha, int gfxid) {
		_pc = pc;
		_cha = cha;
		_gfxid = gfxid;
	}
	
	public void begin() {
		// 効果時間が8秒のため、4秒毎のスキルの場合処理時間を考慮すると実際には1回しか効果が現れない
		// よって開始時間を0.9秒後に設定しておく
		_future = GeneralThreadPool.getInstance().scheduleAtFixedRate(this, 0,
				300);
	}
	
	public void stop() {
		if (_future != null) {
			_future.cancel(false);
		}
	}
	
	@Override
	public void run() {
		if (_cha == null || _cha.isDead()) {
			stop();
			return;
		}
		spawnEffect();
		_timeCounter++;
		if (_timeCounter >= 1) {
			stop();
			return;
		}
	}
	
	private void spawnEffect() {
		S_EffectLocation packet = new S_EffectLocation(_cha.getX(),
				_cha.getY(), _gfxid);
		_pc.sendPackets(packet);
		_pc.broadcastPacket(packet);
	}
}